/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.comp.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nmf.dao.gen.bean.BizCompany;
import com.nmf.site.basedata.category.enums.CategoryType;
import com.nmf.site.basedata.category.service.BaseCategoryService;
import com.nmf.site.biz.comp.service.BizCompanyService;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;

/**
 * 公司Controller
 * @author 北冥
 * @version 2018-12-26
 */
@Controller
@RequestMapping(value = "${adminPath}/comp/bizCompany")
public class BizCompanyController extends BaseController {

	@Autowired
	private BizCompanyService bizCompanyService;
	@ModelAttribute
	public BizCompany get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizCompany entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizCompanyService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizCompany();
		}
		return entity;
	}
	
	@RequiresPermissions("comp:bizCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizCompany bizCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizCompany> page = bizCompanyService.findPage(new Page<BizCompany>(request, response), bizCompany); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/comp/bizCompanyList";
		}
		return "biz/comp/bizCompanyList";
	}
	
	@RequiresPermissions("comp:bizCompany:view")
	@RequestMapping(value = "form")
	public String form(BizCompany bizCompany, Model model) {
		model.addAttribute("bizCompany", bizCompany);
		return "biz/comp/bizCompanyForm";
	}

	@RequiresPermissions("comp:bizCompany:edit")
	@RequestMapping(value = "save")
	public String save(BizCompany bizCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizCompany)){
			return form(bizCompany, model);
		}
		try{
			bizCompanyService.save(bizCompany);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/comp/bizCompany/?repage";
		}
		addMessage(redirectAttributes, "保存公司成功");
		return "redirect:"+Global.getAdminPath()+"/comp/bizCompany/?repage";
	}
	
	@RequiresPermissions("comp:bizCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(BizCompany bizCompany, RedirectAttributes redirectAttributes) {
		try{
			bizCompanyService.delete(bizCompany);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/comp/bizCompany/?repage";
		}
		addMessage(redirectAttributes, "删除公司成功");
		return "redirect:"+Global.getAdminPath()+"/comp/bizCompany/?repage";
	}
	
	
}